package com.weishi.tcp;

import com.weishi.common.utils.GPSUtil;
import com.weishi.common.utils.StringUtils;
import com.weishi.framework.config.BeanContext;
import com.weishi.project.speedometer.service.ISpeedometerService;
import com.weishi.project.speedometer.service.impl.SpeedometerServiceImpl;
import com.weishi.project.training.domain.ShipSpeedRecordLog;
import com.weishi.project.training.vo.ShipSpeedVO;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

public class ThreadSocket implements Runnable {

    private ISpeedometerService speedometerService;

    private Logger logger = Logger.getLogger("TcpService");

    private Socket sock;

    public ThreadSocket(Socket sock) {
        this.sock = sock;
        this.speedometerService = BeanContext.getApplicationContext().getBean(ISpeedometerService.class);

    }

    @Override
    public void run() {
        InputStream input = null;
        byte[] buf = new byte[1024];
        while (true) {
            try {
                input = sock.getInputStream();
				Thread.sleep(200);
			} catch (Exception e) {
				e.printStackTrace();
			}

            if (input != null) {
                int len = 0;
                try {
                    len = input.read(buf);
                    String lens = new String(buf, 0, len);
                    logger.info(lens);
                    // 存储经纬度
                   /* if(lens!=null){
                        String[] retArrays = null;
                        try {
                            retArrays = lens.split("\t|\r|\n");
                            for(String ss : retArrays){
                                List<String> reList = StringUtils.splits(ss);
                                if(reList.size()==17){
                                    //实时数据
                                    ShipSpeedRecordLog shipSpeedRecordLog = new ShipSpeedRecordLog();
                                    shipSpeedRecordLog.setShipSpeedSn(reList.get(0).replace("\"",""));
                                    shipSpeedRecordLog.setUtc(reList.get(3));
                                    shipSpeedRecordLog.setLatitude(GPSUtil.changeDate(Double.parseDouble(reList.get(4))));
                                    shipSpeedRecordLog.setLongitude(GPSUtil.changeDate(Double.parseDouble(reList.get(6))));
                                    speedometerService.addRealTimeData(shipSpeedRecordLog);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            continue;
                        }
                    }*/

				} catch (IOException e) {
					//e.printStackTrace();
				}
                /**
                 * 程序中buf应该是字符数组（char[]）、字节数组（byte[]）、
                 * 整型数组（int[]）吧，意思就是从数组中取出从下标0开始的l
                 * en个元素来生成字符串对象，打印
                 */
            }

        }
    }
}
